package com.lagou.edu.support;

import com.lagou.edu.annotation.Autowired;
import com.lagou.edu.annotation.Component;
import com.lagou.edu.annotation.Qualifer;
import com.lagou.edu.annotation.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Logger;

/**
 * TODO: 事务管理器
 * @author: wyj
 * @date: 2021/11/17 22:53
 **/
@Component
public class TransactionManager {
    private static final Logger LOGGER=Logger.getLogger("TransactionManager:");

    @Autowired
    // @Qualifer(value = "connectionUtil")
    private ConnectionUtil connectionUtil;

    public void open() throws SQLException {
        connectionUtil.getCurrentConnection().setAutoCommit(false);
    }

    public void commit() throws SQLException {
        Connection connection = connectionUtil.getCurrentConnection();
        LOGGER.info("commit-->connection="+connection);
        connection.commit();
    }

    public void rollback() throws SQLException {
        Connection connection = connectionUtil.getCurrentConnection();
        LOGGER.info("rollback-->connection="+connection);
        connection.rollback();
    }

}
